package com.guaoran.common.utils;

import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Author gucheng
 * @Description
 * 图片文件
 * 2019-04-12 17:24
 */
public class PictureUtil {
    public static String getFilename(String url){
        String filename = StringTools.getTimeMillisSequence();
        try {
            String suffix = url.substring(url.lastIndexOf("."), url.length());
            if(suffix.indexOf("/")>0){
                suffix = ".jpg";
            }
            //产生唯一序列号
            filename +=  suffix;
        }catch (Exception e){
            e.printStackTrace();
        }
        return filename;
    }

    public static void main(String[] args) {
//		String content =
//				"<p><br/></p><p><img src=\"http://localhost:8081/DpManage/\\upload\\test2.jpg\" title=\"\" alt=\"\"/><img src=\"http://localhost:8081/DpManage/\\upload\\20180412152423835-Z0000020522796410036.jpg\" title=\"\" alt=\"\"/></p><p><img src=\"http://localhost:8081/DpManage/\\upload\\20180412152503185-Z0000020562146476760.jpg\" title=\"\" alt=\"\"/></p>";
//		String content2 =
//				"<p>liujjliujjliujjliujjliujj<img src=\"http://localhost:8080/DpManage/\\upload\\test2.jpg\" title=\"\" alt=\"\"/></p>";
//		String content3 =
//				"<p><img src=\"http://p3.pstatp.com/large/pgc-image/RGPs6XJ2Uxnu5v\" alt=\"布瑞克县域新经济合伙人招募令 | 农业大数据引燃县域新经济“闪爆点”\" style=\"-webkit-tap-highlight-color: transparent; box-sizing: border-box; border-style: none; max-width: 100%; display: block; margin: 10px auto; color: rgb(34, 34, 34); font-family: \"/></p><p style=\"-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin-top: 16px; margin-bottom: 16px; padding: 0px; color: rgb(34, 34, 34); font-family: \">布瑞克县级农业大数据平台产品，是<span style=\"-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-weight: 700;\">基于布瑞克10年积累，超过10亿条、覆盖200多个国家和地区的农业数据，1亿多条农业咨询信息以及1.6万余篇农业分析报告</span>等组成的布瑞克“农业数据大脑”建立而成。可为县域提供<span style=\"-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-weight: 700;\">4项产品应用</span>，包括大数据智能终端、手机应用系列、WEB网站系列、大数据管理系统；<span style=\"-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-weight: 700;\">17项内容模块</span>，包括市场产业数据库、农业政策数据库、农技资源数据库、专家档案数据库等；<span style=\"-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-weight: 700;\">4项服务优化</span>，包括数据分析及研究支持、大数据普惠金融、产业规划招商服务、人才培训服务等。</p><p><img src=\"http://p1.pstatp.com/large/pgc-image/RGPs6XV7O2dDKj\" alt=\"布瑞克县域新经济合伙人招募令 | 农业大数据引燃县域新经济“闪爆点”\" style=\"-webkit-tap-highlight-color: transparent; box-sizing: border-box; border-style: none; max-width: 100%; display: block; margin: 10px auto; color: rgb(34, 34, 34); font-family: \"/></p><p style=\"-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin-top: 16px; margin-bottom: 16px; padding: 0px; color: rgb(34, 34, 34); font-family: \">未来，<span style=\"-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-weight: 700;\">布瑞克县域智慧农业生态圈将覆盖全国2000+县域。</span><span style=\"-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-weight: 700;\">我们计划面向</span><span style=\"-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-weight: 700;\">全国招募布瑞克县域农业大数据合作伙伴</span><span style=\"-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-weight: 700;\">，为县域新经济产业赋能，现诚挚邀请您加入布瑞克县域合伙人。</span></p><p style=\"-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin-top: 16px; margin-bottom: 16px; padding: 0px; color: rgb(34, 34, 34); font-family: \"><span style=\"-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-weight: 700;\">县域合伙人申请流程</span></p><p style=\"-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin-top: 16px; margin-bottom: 16px; padding: 0px; color: rgb(34, 34, 34); font-family: \">1、提交申请。填写备案人申请表；</p><p style=\"-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin-top: 16px; margin-bottom: 16px; padding: 0px; color: rgb(34, 34, 34); font-family: \">2、初步沟通。工作人员将向您发送相关材料，详细介绍我们公司的农业大数据产品；</p><p style=\"-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin-top: 16px; margin-bottom: 16px; padding: 0px; color: rgb(34, 34, 34); font-family: \">3、明确合作意向。经过您对布瑞克农业大数据产品的深入了解，明确合作意向；</p><p style=\"-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin-top: 16px; margin-bottom: 16px; padding: 0px; color: rgb(34, 34, 34); font-family: \">4、签订协议。合作意向确认后，签署业务合伙协议。</p><p style=\"-webkit-tap-highlight-color: transparent; box-sizing: border-box; margin-top: 16px; margin-bottom: 16px; padding: 0px; color: rgb(34, 34, 34); font-family: \"><span style=\"-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-weight: 700;\">布瑞克合伙人将负责所在地区的项目线索导入和对接，推动项目谈判、签约、立项、招投标、实施推进和资金支付；布瑞克合伙人还可以接受布瑞克委托在本地进行商务推介，及时向布瑞克报备信息，持续参与在地运营。布瑞克提供本地大数据调研及上线运营准备，负责本地化运营工作，并开展县域合伙人培训。县域合伙人将获得该项目的利润分成。</span></p><p><span style=\"-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-weight: 700;\">县域合伙人报名表：<a href=\"http://skeqxkxk3vhr699y.mikecrm.com/DXrWXoQ\" target=\"_self\" style=\"font-weight: bold; white-space: normal;\">http://skeqxkxk3vhr699y.mikecrm.com/DXrWXoQ</a></span></p><p><br/></p>";
//		List<String> list = getImageSrc(content);
//		List<String> list2 = getImageSrc(content2);
//		List<String> list3 = getImageSrc(content3);
//		List<String> arr = new ArrayList<>(list);
//		arr.addAll(list2);
//		arr.addAll(list3);
//		for (String str: arr) {
//			System.out.println(str);
//		}
        String url = "http://dpmanage.agdata.cn:80//upload/，.png";
//		System.out.println(getFilename(url));
//		String str = url.replaceAll("\\\\","/");
//		System.out.println(str);
        downloadPicture(url,"F://xsdx.jpg");
    }
    public static String imgAddr(String htmlCode){
        List<String> list = getImageSrc(htmlCode);
        if(list.size()>0){
            return list.get(0);
        }
        return null;
    }
    public static List<String> getImageSrc(String htmlCode) {
        List<String> imageSrcList = new ArrayList<String>();
        Pattern p = Pattern.compile("<img\\s*([^>]*)\\s*src=\\\"(.*?)\\\"\\s*([^>]*)>", Pattern.CASE_INSENSITIVE);
        Matcher m = p.matcher(htmlCode);
        String quote = null;
        String src = null;
        while (m.find()) {
            quote = m.group(1);
            // src=https://sms.reyo.cn:443/temp/screenshot/zY9Ur-KcyY6-2fVB1-1FSH4.png
            src = (quote == null || quote.trim().length() == 0) ? m.group(2).split("\\s+")[0] : m.group(2);
            imageSrcList.add(src);

        }
        return imageSrcList;
    }
    //链接url下载图片
    private static boolean downloadPicture(String urlList,String path) {
        URL url = null;
        try {
//			urlList = URLEncoder.encode(urlList,"utf-8");
            url = new URL(urlList);
//			String urlhost = url.getProtocol()+"://"+url.getHost()+"/"+URLEncoder.encode(url.getPath(),"utf-8");
//			url = new URL(urlhost);
            DataInputStream dataInputStream = new DataInputStream(url.openStream());

            FileOutputStream fileOutputStream = new FileOutputStream(new File(path));
            ByteArrayOutputStream output = new ByteArrayOutputStream();

            byte[] buffer = new byte[1024];
            int length;

            while ((length = dataInputStream.read(buffer)) > 0) {
                output.write(buffer, 0, length);
            }
            fileOutputStream.write(output.toByteArray());
            dataInputStream.close();
            fileOutputStream.close();
            return true;
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(urlList+"---->"+path);
        return false;
    }

}
